package studychinese.activity;

import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hschinese.pronunciation.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import studychinese.callback.StringDialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.ConFigTag;
import studychinese.config.Url;
import studychinese.model.UserBean;
import studychinese.utils.ToastUtils;

public class SetPassActivity extends BaseAcitivity implements View.OnClickListener {
   private EditText pass_edit_my;
    private EditText newpass_edit_my;
    private EditText repepass_edit_my;
    private ImageView pass_showimg;
    private ImageView pass_unshowimg;
    private ImageView newpass_showimg;
    private ImageView newpass_unshowimg;
    private ImageView repepass_showimg;
    private ImageView repepass_unshowimg;
    private ImageView pass_img_confinrm;
    private ImageView backsetpass;
    private RelativeLayout point;
    private RelativeLayout point2;
    private RelativeLayout point3;


    @Override
    protected int setContainer() {
        return R.layout.activity_set_pass;
    }

    @Override
    public void innitView()
    {
        pass_edit_my = (EditText) findViewById(R.id.ed_pass_my);
        newpass_edit_my = (EditText) findViewById(R.id.ed_newpass_my);
        repepass_edit_my = (EditText) findViewById(R.id.ed_repepass_my);
        pass_showimg = (ImageView) findViewById(R.id.iv_pass_showimg);
        pass_unshowimg = (ImageView) findViewById(R.id.iv_pass_unshowimg);
        newpass_showimg = (ImageView) findViewById(R.id.iv_newpass_showimg);
        newpass_unshowimg = (ImageView) findViewById(R.id.iv_newpass_unshowimg);
        repepass_showimg = (ImageView) findViewById(R.id.iv_repepass_showimg);
        repepass_unshowimg = (ImageView) findViewById(R.id.iv_repepass_unshowimg);
        pass_img_confinrm = (ImageView) findViewById(R.id.iv_pass_confinrm);
        point2 = (RelativeLayout) findViewById(R.id.rl_setpas_point2);
        point = (RelativeLayout) findViewById(R.id.rl_setpas_point);
        point3 = (RelativeLayout) findViewById(R.id.rl_setpas_point3);
        backsetpass = (ImageView) findViewById(R.id.iv_back_setpass);
        backsetpass.setOnClickListener(this);
        pass_showimg.setOnClickListener(this);
        pass_unshowimg.setOnClickListener(this);
        newpass_showimg.setOnClickListener(this);
        newpass_unshowimg.setOnClickListener(this);
        repepass_showimg.setOnClickListener(this);
        repepass_unshowimg.setOnClickListener(this);
        pass_img_confinrm.setOnClickListener(this);

        OnFous();

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_pass_showimg:

                pass_showimg.setVisibility(View.GONE);
                pass_unshowimg.setVisibility(View.VISIBLE);
                pass_edit_my.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

                break;
            case R.id.iv_pass_unshowimg:
                pass_showimg.setVisibility(View.VISIBLE);
                pass_unshowimg.setVisibility(View.GONE);
                pass_edit_my.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case R.id.iv_newpass_showimg:
                newpass_showimg.setVisibility(View.GONE);
                newpass_unshowimg.setVisibility(View.VISIBLE);
                newpass_edit_my.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                break;
            case R.id.iv_newpass_unshowimg:
                newpass_showimg.setVisibility(View.VISIBLE);
                newpass_unshowimg.setVisibility(View.GONE);
                newpass_edit_my.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case R.id.iv_repepass_showimg:
                repepass_showimg.setVisibility(View.GONE);
                repepass_unshowimg.setVisibility(View.VISIBLE);
                repepass_edit_my.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                break;
            case R.id.iv_repepass_unshowimg:
                repepass_showimg.setVisibility(View.VISIBLE);
                repepass_unshowimg.setVisibility(View.GONE);
                repepass_edit_my.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case R.id.iv_pass_confinrm:
                String oldPwd = pass_edit_my.getText().toString();
              String  newPwd1 = newpass_edit_my.getText().toString();
                String newPwd2 = repepass_edit_my.getText().toString();
                //int length = this.s.length();
                if ("".equals(oldPwd)||"".equals(newPwd1)||"".equals(newPwd2))
                {
                     Toast.makeText(SetPassActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                }

                else
                {
                  if (!newPwd1.equals(newPwd2))
                  {
                      Toast.makeText(SetPassActivity.this,"两次密码不一致请重新输入",Toast.LENGTH_SHORT).show();
                  }
                  else
                  {
                     NetDate(oldPwd,newPwd1);

                  }
                }

                break;
            case R.id.iv_back_setpass:
                finish();
                break;
        }
    }


    private void NetDate(String oldPwd,String new_passwd )
    {       String user_id = UserBean.getInfor().getUser_id();
        OkGo.<String>post(AppUrl.getAppUrl(Url.REGISTER_UPDATEPASSWD)).params("user_id",user_id).params("user_passwd",oldPwd)
                .params("new_passwd",new_passwd).execute(new StringDialogCallback(this) {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject   obj = new JSONObject(response.body());
                    String  state = obj.getString("state");
                    String message = obj.getString("message");
                    if (state.equals(ConFigTag.state))
                    {
                        SetPassActivity.this.finish();


                    }
                    else
                    {
                        ToastUtils.showShort(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }

    private void OnFous()
    {
        pass_edit_my.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                }
                else
                {
                    int length = pass_edit_my.getText().length();
                    if (length<6)
                    {
                        point.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        point.setVisibility(View.GONE);
                    }
                }

            }
        });

        newpass_edit_my.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                }
                else
                {
                    int length = newpass_edit_my.getText().length();
                    if (length<6)
                    {
                        point2.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        point2.setVisibility(View.GONE);
                    }
                }

            }
        });
        repepass_edit_my.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                }
                else
                {
                    int length = repepass_edit_my.getText().length();
                    if (length<6)
                    {
                        point3.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        point3.setVisibility(View.GONE);
                    }
                }

            }
        });

    }
}
